package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.ClassificationOfCulture;
import com.ruoyi.system.service.IClassificationOfCultureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 养殖种类分类Controller
 *
 * @author ruoyi
 * @date 2024-05-13
 */
@Api("海底滩涂信息接口")
@RestController
@RequestMapping("/system/culture")
public class ClassificationOfCultureController extends BaseController {
    @Autowired
    private IClassificationOfCultureService classificationOfCultureService;

    /**
     * 查询养殖种类分类列表
     */
    @ApiOperation("查询海底滩涂信息列表")
    @GetMapping("/list")
    public TableDataInfo list(ClassificationOfCulture classificationOfCulture) {
        List<ClassificationOfCulture> list = classificationOfCultureService.selectClassificationOfCultureList(classificationOfCulture);
        buildHierarchy(list);
        list.removeIf(obj -> obj.getParentId() != 0);
        return getDataTable(list);
    }

    /**
     * 查询养殖种类分类列表
     */
    @ApiOperation("查询海底滩涂信息列表")
    @GetMapping("/queryList")
    public TableDataInfo queryList() {
        ClassificationOfCulture classificationOfCulture = new ClassificationOfCulture();
        List<ClassificationOfCulture> list = classificationOfCultureService.selectClassificationOfCultureList(classificationOfCulture);
        return getDataTable(list);
    }

    /**
     * 导出养殖种类分类列表
     */
    @ApiOperation("查询海底滩涂信息列表")

    @Log(title = "养殖种类分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ClassificationOfCulture classificationOfCulture) {
        List<ClassificationOfCulture> list = classificationOfCultureService.selectClassificationOfCultureList(classificationOfCulture);
        ExcelUtil<ClassificationOfCulture> util = new ExcelUtil<ClassificationOfCulture>(ClassificationOfCulture.class);
        util.exportExcel(response, list, "养殖种类分类数据");
    }

    /**
     * 获取养殖种类分类详细信息
     */
    @ApiOperation("获取养殖种类分类详细信息")

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(classificationOfCultureService.selectClassificationOfCultureById(id));
    }

    /**
     * 新增养殖种类分类
     */
    @ApiOperation("新增养殖种类分类")

    @Log(title = "养殖种类分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClassificationOfCulture classificationOfCulture) {
        return toAjax(classificationOfCultureService.insertClassificationOfCulture(classificationOfCulture));
    }

    /**
     * 修改养殖种类分类
     */
    @ApiOperation("修改养殖种类分类")

    @Log(title = "养殖种类分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ClassificationOfCulture classificationOfCulture) {
        return toAjax(classificationOfCultureService.updateClassificationOfCulture(classificationOfCulture));
    }

    /**
     * 删除养殖种类分类
     */
    @ApiOperation("删除养殖种类分类")
    @Log(title = "养殖种类分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(classificationOfCultureService.deleteClassificationOfCultureByIds(ids));
    }

    public void buildHierarchy(List<ClassificationOfCulture> list) {
        // 首先找到所有的根节点，即parentId为0的节点
        List<ClassificationOfCulture> roots = new ArrayList<>();
        for (ClassificationOfCulture obj : list) {
            if (obj.getParentId() == 0) {
                roots.add(obj);
            }
            obj.setLabel(obj.getName());
        }

        // 为每个根节点递归构建子树
        for (ClassificationOfCulture root : roots) {
            addChildren(root, list);
        }
    }

    private void addChildren(ClassificationOfCulture parent, List<ClassificationOfCulture> list) {
        for (ClassificationOfCulture obj : list) {
            if (obj.getParentId().equals(parent.getId())) {
                if (parent.getChildren() == null) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(obj);
                // 递归地为当前节点添加子节点
                addChildren(obj, list);
            }
        }
    }
}
